package com.lovi.service;


import com.lovi.dao.DiningTableDao;
import com.lovi.domain.DiningTable;
import com.lovi.utils.ScannerUtils;

import java.util.List;

public class DiningTableService {

    private DiningTableDao diningTableDao = new DiningTableDao();


    /**
     * 根据id查询餐桌
     * @param diningTableId
     * @return
     */
    public DiningTable getDiningTableById(int diningTableId) {
        DiningTable diningTable = diningTableDao.getOneById(DiningTable.class, "select * from dinning_table where id =?" , diningTableId);
        return diningTable;
    }

    /**
     * 预定餐桌
     */
    public boolean orderDiningTable(String empId) {
        System.out.println("=========预定餐桌==========");
        System.out.print("请输入要预定的餐桌编号(-1退出): ");
        Integer tableId =  ScannerUtils.readInt(10);
        if (tableId == -1) {
            System.out.println("你退出了订座操作");
            return false;
        }
        DiningTable table = this.getDiningTableById(tableId);
        if(table == null){
            System.out.println("该餐桌不存在!!!");
            return false;
        }
        if(!"空".equals(table.getState())){
            System.out.println("该餐桌已被占用,请选择其他餐桌!");
            System.out.println("=========预定失败==========");
            return false;
        }
        System.out.println("请输入预定人名称: ");
        String orderName = ScannerUtils.readString(50);
        System.out.println("请输入预定人电话: ");
        String orderTel = ScannerUtils.readString(50);
        // 当可以预定的时候，需要把预订人的名字，电话存入我们的餐桌表
        int i = diningTableDao.update("update dinning_table set orderId = ?, " +
                "state = '已被预订',orderName = ?,orderTel  = ? where id = ?", empId,orderName, orderTel, tableId);
        return i>0;
    }

    /**
     * 显示餐桌状态
     * @return
     */
    public List<DiningTable> list() {
        List<DiningTable> list = diningTableDao.list(DiningTable.class, "select * from dinning_table", null);
        return list;
    }
}
